GuildFightModel = {}
local this = GuildFightModel

GuildFightModel.recordData = nil
GuildFightModel.rewardData = nil

function GuildFightModel.GetNPC()
    local MyRace = G.serverData.roleInfo.ability.race
    this.NPC_X = MyRace + 2
    this.NPC_Y = 0
end
--获得图腾NPC
function GuildFightModel.GetTotem()
    for k,v in pairs(G.serverData["mapInfo"].npcList) do
        if v.NpcShow ~= nil and v.NpcShow.npcId == 103 then
            this.NPC_X = v.npcx
            this.NPC_Y = v.npcy
            return
        end
    end
    logError("此地图找不到公会战图腾！")
end

---请求加入工会战
function GuildFightModel.addGuildFight()
    this.GetNPC()
    SendMsg.Send(Config.msg_type.send_addGuildFight,0,this.NPC_X,this.NPC_Y)
    UIWaiting.HideWaiting("0x1623");
    local name = getProtocol(Config.msg_main_type.main_arena,Config.sub_type_arena.sub_addGuildFight)
    CloseEvent(name);
    ListenEvent(name,function (d)
        --printTable(d)
        UISysTips.AddMsg(d.content)
    end )
end

---请求退出公会战
function GuildFightModel.exitGuildFight()
    this.GetNPC()
    SendMsg.Send(Config.msg_type.send_exitGuildFight,0,this.NPC_X,this.NPC_Y)
    local name = getProtocol(Config.msg_main_type.main_arena,Config.sub_type_arena.sub_exitGuildFight)
    CloseEvent(name);
    ListenEvent(name,function (d)
        --printTable(d)
        if d.content ~= nil and d.content ~= "" then
            UISysTips.AddMsg(d.content)
        end
        GuildFightModel.getGuildFightState()
    end )
end
---请求公会战献祭列表
function GuildFightModel.ItemListGuildFight()
    this.GetTotem()
    SendMsg.Send(Config.msg_type.send_ItemListGuildFight,0,this.NPC_X,this.NPC_Y)
    local name = getProtocol(Config.msg_main_type.main_arena,Config.sub_type_arena.sub_ItemListGuildFight)
    CloseEvent(name);
    ListenEvent(name,function (d)
        --printTable(d)
        GuildWarManager.OfferList(d)
    end )
end
---请求公会战献祭,id献祭的编号
function GuildFightModel.AddItemGuildFight(id)
    this.GetTotem()
    SendMsg.Send(Config.msg_type.send_AddItemGuildFight,0,this.NPC_X,this.NPC_Y,id)
    local name = getProtocol(Config.msg_main_type.main_arena,Config.sub_type_arena.sub_AddItemGuildFight)
    CloseEvent(name);
    ListenEvent(name,function (d)
        --printTable(d)
        if d.content ~= nil then
            UISysTips.AddMsg(d.content)
        end
        if d.opt == 0 then
            GuildFightModel.mapInfoGuildFight()
        end
    end )
end
---请求查看当前地图公会献祭信息
function GuildFightModel.mapInfoGuildFight()
    this.GetTotem()
    SendMsg.Send(Config.msg_type.send_mapInfoGuildFight,0,this.NPC_X,this.NPC_Y)
    local name = getProtocol(Config.msg_main_type.main_arena,Config.sub_type_arena.sub_mapInfoGuildFight)
    CloseEvent(name);
    ListenEvent(name,function (d)
        --printTable(d)
        GuildWarManager.mapInfoCallBack(d)
    end )
end
---请求查看公会战战绩
function GuildFightModel.recordGuildFight()
    SendMsg.Send(Config.msg_type.send_recordGuildFight,0)
    local name = getProtocol(Config.msg_main_type.main_arena,Config.sub_type_arena.sub_recordGuildFight)
    CloseEvent(name)
    ListenEvent(name,function (d)
        --printTable(d)
        this.recordData = d
        UIGuildWarRecord.UpdateCallBack()
    end )
end
---请求查看公会战奖励
function GuildFightModel.rewardGuildFight()
    SendMsg.Send(Config.msg_type.send_rewardGuildFight,0)
    local name = getProtocol(Config.msg_main_type.main_arena,Config.sub_type_arena.sub_rewardGuildFight)
    CloseEvent(name)
    ListenEvent(name,function (d)
        --printTable(d)
        this.rewardData = d
    end )
end

---请求查看公会战状态
function GuildFightModel.getGuildFightState()
    SendMsg.Send(Config.msg_type.send_getGuildFightState,0)
    local name = getProtocol(Config.msg_main_type.main_arena,Config.sub_type_arena.sub_GuildFightState)
    CloseEvent(name)
    ListenEvent(name,function (d)
        --printTable(d)
        InstanceManager.SetGuildFightMode(d.opt == 4)
    end )
end

return GuildFightModel